home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Skel 3.0i / Skel setup.p < prev    next >
Text File  |  1994-05-15  |  6KB  |  190 lines

  1. unit Setup;
  2.  
  3. interface
  4.     uses
  5.         Globals;
  6.  
  7.     procedure Setup;
  8.  
  9. implementation
  10.  
  11. {SetUps for handling memory}
  12. {##########################   SetUpMemory   ############################}
  13. {     This very important set of initializations can be left out of the}
  14. {   first versions of a program. We are making sure that memory is laid}
  15. {   out as we desire, with adequate protection against running out of}
  16. {   memory, bad handles, etc.}
  17.  
  18.     procedure SetupMemory; {NOT USED}
  19.         const
  20.             maxStackSize = 8192;    (* max size of stack; the heap gets the rest *)
  21.         type
  22.             lomemptr = Ptr;    (* a pointer to low memory locations *)
  23.         var
  24.             nilptr: lomemptr;        (* will have value NIL *)
  25.             stackbaseptr: lomemptr;    (* points to current stack base *)
  26.     begin
  27.  
  28. {  If you define a GrowZone function to Handle bad memory problems,}
  29. {  you should define it at the top level (not nested), and set it}
  30. {  here. We don't.}
  31.  
  32. (*     SetGrowZone(&mygrowzone); *)
  33.  
  34. {  Place a longint -1 (an odd and therefore illegal address) in the}
  35. {  memory location that would be referenced by an accidentally-NULL}
  36. {  Handle, so the error will be caught at Handle-reference time (as}
  37. {  an Address error, ID=02) instead of later on.}
  38.  
  39. {NOTE: This hardy helps any more, at least not the odd address, since that's legal}
  40. {nowadays (on 68020 and better).}
  41.  
  42.         nilptr := nil;
  43.         nilptr^ := LongInt(-1);
  44.  
  45. {  If you needed to use an Application heap limit other than the}
  46. {  default (which allows 8K for the stack), you'd set it here,}
  47. {  possible using this technique of explicitly specifying the maximum}
  48. {  stack size and allocating the rest to the heap.  Should be}
  49. {  independent of memory size. }
  50.  
  51.         stackbaseptr := lomemptr($908);
  52.                 (* CurStackBase from Tlasm/sysequ.text *)
  53.         SetApplLimit(Ptr(stackbaseptr^ - maxStackSize));
  54.  
  55. {  Expand the application heap Zone to its maximum size, without}
  56. {  purging any purgeable resources.  This saves memory compactions}
  57. {  and heap expansions later.}
  58.  
  59.         MaxApplZone;
  60.  
  61. {  get plenty of master pointers now; if we let the Memory Manager}
  62. {  Allocate them as needed, they'd form non-relocatable islands in}
  63. {  the heap.}
  64.  
  65.         MoreMasters;
  66.         MoreMasters;
  67.         MoreMasters;
  68.  
  69. {  Here you might install bulwarks against running out of memory}
  70. {  unexpectedly.  One such (cheesy) technique is to here Allocate}
  71. {{  a large Handle, call it "CheeseBuf", which you can de-Allocate}
  72. {  in your GrowZone function, when you must obtain more memory to}
  73. {  avoid a crash.  While de-allocated, the program could prevent}
  74. {  the user from doing anything requiring memory, and tell him he}
  75. {  must discard windows or some such memory freeing action. Each}
  76. {  time he does so, the program can try to re-Allocate CheeseBuf;}
  77. {  if it succeeds, the user can go on doing memory-eating operations.}
  78.  
  79.     end;                (* SetUpMemory *)
  80.  
  81.  
  82. {############################   SetUpMenus   #############################}
  83.  
  84. {    Once-only initialization for menus}
  85. {    We read in all menus from the resource file, and install them,}
  86. {    and all desk accessories (drivers).}
  87.  
  88.     procedure SetupMenus;
  89.         var
  90.             i: Integer;
  91.     begin
  92.         for i := 1 to lastmenu do        (* get all my menus in *)
  93.             mymenus[i] := GetMenu(i);    (* use the fact that our menu ID's start at 1 *)
  94. {mymenus[applemenu] := NewMenu(applemenu, char(24));}
  95. {AppendMenu(mymenus[applemenu], 'About Skel…;-');}
  96.         AddResMenu(mymenus[applemenu], 'DRVR');
  97.                 (* pull in all desk accessories *)
  98.         for i := 1 to lastmenu do
  99.             InsertMenu(mymenus[i], 0);
  100.         DrawMenuBar;
  101.     end;
  102.  
  103.  
  104. {body of SetUp}
  105. {Once-only initialization for Skel}
  106. {############################   SetUp   ##############################}
  107.  
  108. {   Initialize our program.  It seems best to Handle:}
  109. {   Memory inits first, ToolBox inits second, then the program variables'}
  110. {   inits. Note that the order of inits is important; see "Using the}
  111. {   Dialog Manager" in the Dialog Mgr section.}
  112.  
  113.     procedure Setup;
  114.         var
  115.             screenrect: Rect;
  116.             theWorld: SysEnvRec;
  117.     begin
  118. {  The following function is not declared in <win.h>, although}
  119. {  that is where you would expect it.  - WHJ 3/10/85}
  120.  
  121. {SetupMemory unnecessary: Think Pascal initializes things the proper way.}
  122. {You may want to put some of it back in, though - particularly a few MoreMasters}
  123. {setupmemory;    (*  init memory layout and protection *)
  124.  
  125. (*  init QuickDraw, and everybody else *)
  126. {NOT needed - Think Pascal does it for us}
  127.  
  128. {InitGraf(thePort);}
  129. {InitFonts;}
  130. {InitWindows;}
  131. {InitMenus;}
  132. {TEInit;}
  133. {InitDialogs(nil);}
  134.  
  135. {Flush events, but don't flush disk events.}
  136.         FlushEvents(everyEvent - diskMask, 0);
  137.  
  138. {  Init the system event mask, in case the previous program left}
  139. {  it in a bad state.  If you set it non-standard here, FIX IT}
  140. {  BEFORE EXITING, because the Finder (1.1g) does NOT set it.}
  141.  
  142. (*    SetEventMask (everyEvent - keyUpMask);*)
  143. (* standard setting *)
  144.  
  145. {  Get the port which is the whole screen, to use when deactivating}
  146. {  our window.  This prevents the current GrafPort pointer from}
  147. {  ever dangling.}
  148.  
  149.         GetWMgrPort(screenport);    (* get whole screen port that window mgr uses *)
  150.         SetPort(screenport);    (* and start off with it *)
  151.  
  152. {Check some system features /LIR}
  153.         gWNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
  154.         if SysEnvirons(1, theWorld) = noErr then
  155.             gColorQDFlag := theWorld.hasColorQD;
  156.  
  157. {  get window: use wRecord storage.  Port is set to that of the}
  158. {  new window.}
  159. {  GetNewWindow posts an update event for the new window,}
  160. {  so it will be redrawn right away.}
  161. {Get a color window if CQD is available! /LIR}
  162.  
  163.         if gColorQDFlag then
  164.             mywindow := GetNewCWindow(windowid, @wrecord, WindowPtr(-1))
  165.         else
  166.             mywindow := GetNewWindow(windowid, @wrecord, WindowPtr(-1));
  167.                 (* -1 => frontmost window *)
  168.  
  169. (* set up dragRect; we can drag the window within it *)
  170.         screenrect := screenBits.bounds; (* don't assume screen size *)
  171.  
  172. (*  set drag Rect to avoid menu bar, and keep at least 4 pixels *)
  173. (*  on screen *)
  174.         SetRect(dragrect, 4, 24, screenrect.right - 4, screenrect.bottom - 4);
  175.  
  176. (* set up GrowRect, for limits on window growing *)
  177.         SetRect(growrect, 48, 14, screenrect.right - 7, screenrect.bottom - 7);
  178.  
  179. (* pull in and set up our menus *)
  180.         SetupMenus;
  181.  
  182. {Set some globals that are necessary for the new features /LIR}
  183.         gSleep := 5;
  184.         gAppleEventsInitialized := false;
  185.  
  186. {We have to InitCursor to get rid of the wait cursor.}
  187.         InitCursor;
  188.     end;
  189.  
  190. end.